package com.ckex.learn.lucene.dao.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ckex.learn.lucene.dao.OrderDao;
import com.ckex.learn.lucene.dao.page.PageList;
import com.ckex.learn.lucene.dao.page.PageQuery;
import com.ckex.learn.lucene.dao.page.Paginator;
import com.ckex.learn.lucene.dao.page.SearchMap;
import com.ckex.learn.lucene.domain.OrderDo;


/**
 * @author ckex created 2013-10-7 - 下午2:23:08 OrderDaoImpl.java
 * @explain -
 */
@Repository("orderDao")
public class OrderDaoImpl extends BaseDao implements OrderDao {

    @Override
    public OrderDo load(Long id) {
        return (OrderDo) getSqlMapClientTemplate().queryForObject("t_order.load", id);
    }

    @Override
    public boolean delete(Long id) {
        return getSqlMapClientTemplate().delete("t_order.delete", id) > 0;
    }

    @Override
    public Long create(OrderDo record) {
        return (Long) getSqlMapClientTemplate().insert("t_order.create", record);
    }

    @Override
    public boolean update(OrderDo record) {
        return getSqlMapClientTemplate().update("t_order.update", record) > 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageList<OrderDo> listByPage(Long sellerId, PageQuery pageQuery, Integer count) {
        SearchMap map = new SearchMap();
        map.add("sellerId", sellerId);
        map.add("startIndex", pageQuery.getStartIndex());
        map.add("pageSize", pageQuery.getPageSize());
        if (count == null || count.intValue() == 0) {
            count = (Integer) getSqlMapClientTemplate().queryForObject("t_order.listByPageCount",
                    map);
        }
        List<OrderDo> list = Collections.emptyList();
        if (count != null && count.intValue() > 0) {
            list = (List<OrderDo>) getSqlMapClientTemplate()
                    .queryForList("t_order.listByPage", map);
        }
        Paginator paginator = new Paginator(pageQuery.getPageSize(), count);
        paginator.setPage(pageQuery.getPageNum());
        PageList<OrderDo> result = new PageList<OrderDo>(list);
        result.setPaginator(paginator);
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<OrderDo> listAllByTid(Long sellerId, Long tradeId) {
        SearchMap map = new SearchMap();
        map.add("sellerId", sellerId);
        map.add("tradeId", tradeId);
        return getSqlMapClientTemplate().queryForList("t_order.listAllByTid", map);
    }
}
